package com.ehualu.javahadoop.test; /**
 * @author 吴敬超
 * @date 2018/9/4
 * Description:
 */


import java.sql.*;

public class Hivecheck {


    public String hivenum() {


        Connection con = null;

        Statement stmt = null;

        ResultSet res = null;
        try {
            Class.forName("org.apache.hive.jdbc.HiveDriver");
            con = DriverManager.getConnection("jdbc:hive2://172.16.101.52:10000/default", "hdfs", "hdfs");
            stmt = con.createStatement();


            String sql = "select count(*) as ct from gcsjdb.lcgcsj where dt=20190801 and qx=371503";

            System.out.println(sql);
            res = stmt.executeQuery(sql);

//        System.out.println(res.getString(1));
            while (res.next()) {
//                num = res.getString(1);
                String num = res.getString("ct");

                System.out.println("hive num:" + num);
//                System.out.println("Result: key:" + res.getString(1));

                return num;
            }


        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (res != null) {
                try {
                    res.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

        }


        return "err";

//        return tablename;
    }


/*
    public static void main(String[] args) {


        List<String> muntype = new ArrayList<>();
        muntype.add("TINYINT");
        muntype.add("SMALLINT");
        muntype.add("INT");
        muntype.add("INTEGER");
        muntype.add("BIGINT");

        muntype.add("FLOAT");
        muntype.add("DOUBLE");
        muntype.add("DECIMAL");


        String hivedanum = null;
        String datanum = null;

        List<String> allnums = new ArrayList<>();

        String ddriver = "oracle.jdbc.driver.OracleDriver";

        String url = "jdbc:oracle:thin:@10.1.81.248:1521/ifm30";
        String user = "ifmodsuser";
        String password = "ifmodspwd";
        String tablename = "IFMODSUSER.TBINSURECOMB";

        String hivetbname = "FS_TBINSURECOMB";
        String dati = "2018-09-02";

//        String exemd = "ct";
        try {
            Hivecheck check = new Hivecheck();


//        Map colnametypes = new HashMap<String, String>();

//        Map<String, String> mapnametype = new LinkedHashMap<String, String>();
            List<String[]> listallinfo = check.datype(hivetbname);


            for (String[] listinfo : listallinfo) {

                if (listinfo[0].equals("TX_DT")) {

                    continue;
                }

                if (muntype.contains(listinfo[1])) {

                    hivedanum = check.hivenum(hivetbname, listinfo[0], dati, "sum");
                    datanum = check.danum(ddriver, tablename, url, user, password, listinfo[2], "sum");


                } else {

                    hivedanum = check.hivenum(hivetbname, listinfo[0], dati, "ct");
                    datanum = check.danum(ddriver, tablename, url, user, password, listinfo[2], "ct");


                }
                System.out.println(datanum + "|" + hivedanum);


                allnums.add(datanum + "|" + hivedanum);


                try {
                    if (!hivedanum.equals(datanum)) {
                        System.out.println("*******不一致,hive 字段*******");
                        System.out.println(listinfo[0]);


                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    System.out.println("*******不一致,hive 字段*******");
                }


            }


            System.out.println("*******源字段*******");
            for (String[] listinfo : listallinfo) {

                if (listinfo[0].equals("TX_DT")) {

                    continue;
                }
                System.out.println(listinfo[2]);

            }


            System.out.println("******allnums********");
            for (String allnum : allnums) {

                System.out.println(allnum);
            }


        } catch (Exception e) {
            e.printStackTrace();
        }


    }
*/


}
